package com.yazi.mxz.person.controller.firm;

import java.util.Set;

import javax.annotation.Resource;



import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.myframework.smhj.common.exception.MyException;
import com.myframework.smhj.jackson.result.ObjectResult;
import com.yazi.mxz.common.entity.beans.Role;
import com.yazi.mxz.common.entity.beans.Store;
import com.yazi.mxz.common.entity.beans.StoreMember;
import com.yazi.mxz.common.service.StoreService;
import com.yazi.mxz.person.jackson.filter.RoleFilter;

@Controller("firm-storeController")
@RequestMapping("/firm/store")
public class StoreController extends BaseController {
	private static Logger log = LoggerFactory.getLogger(StoreController.class);
	
	@Resource
	StoreService storeService;
	
	@RequestMapping("/addOrEdit")
	public String addOrEdit(Long storeId, Model model) {
		if (null == storeId) {
			debug(log, "进入店铺添加页面");
		} else {
			debug(log, "进入店铺编辑页面");
			
			try {
				Store store = storeService.one(storeId);
				// 检查要编辑的店铺是否为当前企业店铺
				if (store.getCompany().equals(getCompany(true))) {
					Set<StoreMember> storeMembers = store.getStoreMembers();
					
					model.addAttribute("store", store);
					model.addAttribute("storeMembers", storeMembers);
				} else {
					model.addAttribute(TIP_MSG, "访问异常，请重试");
					return FORWARD_TIP;
				}
			} catch (Exception e) {
				error(log, e, "进入店铺编辑页面");
				model.addAttribute(TIP_MSG, "访问异常，请重试");
				return FORWARD_TIP;
			}
		}
		return "firm/store_addOrEdit";
	}
	
	@RequestMapping("/addOrEditDo/json")
	@ResponseBody
	public ObjectResult<?> addOrEditDoJson(Store store) {
		ObjectResult<Long> jsonResult = new ObjectResult<Long>();
		if (null == store.getStoreId()) {
			debug(log, "保存新添加店铺信息");
			try {
				
				store = storeService.addByCompany(getCompany(true), store);
				
				jsonResult.setResult(store.getStoreId());
				jsonResult.setOkMessage("添加成功，给店铺设置管理员吧");
			} catch (MyException e) {
				jsonResult.setFailMessage(e.getMessage());
			} catch (Exception e) {
				error(log, e, "保存新增店铺信息异常");
				jsonResult.setErrorMessage("新增店铺失败，请联系管理员");
			}
		} else {
			debug(log, "保存编辑店铺信息");
			try {
				store = storeService.editByCompany(getCompany(true), store);
				
				jsonResult.setResult(store.getStoreId());
				jsonResult.setOkMessage("保存成功");
			} catch (MyException e) {
				jsonResult.setFailMessage(e.getMessage());
			} catch (Exception e) {
				error(log, e, "保存编辑店铺信息异常");
				jsonResult.setErrorMessage("保存失败，请联系管理员");
			}
		}
		return jsonResult;
	}
	
	@RequestMapping("/loadRoles/json")
	@ResponseBody
	public ObjectResult<?> loadRolesJson(Long storeId) {
		log.debug("获取店铺管理角色");
		ObjectResult<Set<Role>> jsonResult = new ObjectResult<Set<Role>>();
		jsonResult.setFilter(RoleFilter.get());
		try {
			Store store = storeService.one(storeId);
			Set<Role> roles = store.getRoles();
			
			jsonResult.setResult(roles);
			jsonResult.setOk();
		} catch (Exception e) {
			error(log, e, "获取店铺管理角色异常");
			jsonResult.setErrorMessage("加载店铺管理角色失败");
		}
		return jsonResult;
	}
	
	@RequestMapping("/list")
	public String list(Model model) {
		log.debug("进入店铺列表");
		
		Set<Store> stores = getCompany(true).getStores();
		
		model.addAttribute("stores", stores);
		return "firm/store_list";
	}
}
